package edu.mobbuzz.page.menu.rsschannel;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Border;
import edu.mobbuzz.bean.Category;
import edu.mobbuzz.bean.RSSChannel;
import edu.mobbuzz.facade.CategoryFacade;
import edu.mobbuzz.facade.LabelFacade;
import edu.mobbuzz.facade.RSSChannelFacade;
import edu.mobbuzz.page.menu.Menu;
import edu.mobbuzz.renderer.CategoryCheckBoxRenderer;
import edu.mobbuzz.util.string.MenuString;
import java.util.Vector;


public class RSSChannelChoice extends Menu {
    private Form rssChannelForm;
    private Container rssChannelPanel;
    private Label menuTitle;
    private DefaultListModel categoryListModel;
    private List categoryList;
    private int index;
    private RSSChannel rssChannel;
    private Command cancelCmd, okCmd;
    private CategoryFacade categoryFacade;
    private LabelFacade labelFacade;
    private RSSChannelFacade rssChannelFacade;
    
    public RSSChannelChoice(){
        rssChannelForm = new Form();
        rssChannelPanel = new Container();
        menuTitle = new Label(getMenuTitle());

        categoryListModel = new DefaultListModel(getCategoryFacade().displayCategoryList());
        cancelCmd = new Command("Cancel") {
            public void actionPerformed(ActionEvent evt) {
               backToRSSChannelPage();
            }
        };
        
        okCmd = new Command("Ok") {
            public void actionPerformed(ActionEvent evt) {
                labelingRSSChannel();
            }
        };
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
    
    public void showPage() {
        menuTitle.setAlignment(Component.CENTER);
        menuTitle.getStyle().setMargin(5, 0, 5, 0);
        menuTitle.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        menuTitle.getStyle().setBgTransparency(0);
        
        
        rssChannelPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        rssChannelPanel.addComponent(menuTitle);
        rssChannelPanel.addComponent(createList(categoryListModel, List.VERTICAL, new CategoryCheckBoxRenderer()));
        rssChannelForm.addCommand(cancelCmd);
        rssChannelForm.addCommand(okCmd);

       
        Vector labelArr = getLabelFacade().getLabelByRss(rssChannel.getRecId());
        for(int i=0;i<labelArr.size();i++){
            edu.mobbuzz.bean.Label lbl = (edu.mobbuzz.bean.Label)labelArr.elementAt(i);

            for(int j=0;j<categoryListModel.getSize();j++){
                Category c = (Category)categoryListModel.getItemAt(i);
                if(c.getRecId() == lbl.getRecIdCategory())
                    c.setSelected(true);
            }
            

        }
        rssChannelForm.setLayout(new BorderLayout());
        rssChannelForm.addComponent(BorderLayout.CENTER, rssChannelPanel);
        rssChannelForm.show();
    }

    private List createList(DefaultListModel categoryListModel, int orientation, ListCellRenderer renderer) {
        categoryList = new List(categoryListModel){
            public void keyPressed(int keyCode){
               gameActionPerformed(keyCode);
            }
        };
        categoryList.getStyle().setBgTransparency(0);
        categoryList.getStyle().setBorder(Border.getEmpty());
        categoryList.setListCellRenderer(renderer);
        categoryList.setOrientation(orientation);

        return categoryList;
    }
    
    

    public void gameActionPerformed(int keyCode){
        int game = Display.getInstance().getGameAction(keyCode);

        if(game == Display.GAME_FIRE){
            Category c = (Category)categoryList.getSelectedItem();
            c.setSelected(!c.isSelected());
        }else if(game == Display.GAME_DOWN){
            if(categoryList.getSelectedIndex()<(categoryList.size()-1)){
               categoryList.setSelectedIndex(categoryList.getSelectedIndex()+1);
            }else{
                categoryList.setSelectedIndex(0);
            }

        }else if(game == Display.GAME_UP){
             if(categoryList.getSelectedIndex()!=0){
                 categoryList.setSelectedIndex(categoryList.getSelectedIndex()-1);
             }else{
                 categoryList.setSelectedIndex(categoryList.size()-1);
             }

        }
    }

    public void labelingRSSChannel(){

        for(int i=0;i<categoryListModel.getSize();i++){
            Category category = (Category)categoryListModel.getItemAt(i);
            if(category.isSelected()){
                edu.mobbuzz.bean.Label label = new edu.mobbuzz.bean.Label();
                if(!getLabelFacade().isLabelByRssAndCategory(getRssChannel().getRecId(), category.getRecId())){
                    label.setRecIdCategory(category.getRecId());
                    label.setRecIdRSS(getRssChannel().getRecId());
                    getLabelFacade().addLabel(label);
                }
            }else{
                Vector indexCategoryArr = new Vector();
                indexCategoryArr = getLabelFacade().getDeletedIndexLabelByRssAndCategory(getRssChannel().getRecId(),  category.getRecId());
                for(int j=0;j<indexCategoryArr.size();j++){
                    getLabelFacade().deleteLabel(Integer.parseInt(indexCategoryArr.elementAt(i).toString()));
                }
            }
        }         
        backToRSSChannelPage();
      
    }

   
    public void backToRSSChannelPage() {
        RSSChannelPage rssChannelPage = new RSSChannelPage();
        rssChannelPage.setExitCmd(getExitCmd());
        rssChannelPage.setActionlistener(getActionlistener());        
        rssChannelPage.showPage();

    }

    public RSSChannel getRssChannel() {
        return rssChannel;
    }

    public void setRssChannel(RSSChannel rssChannel) {
        this.rssChannel = rssChannel;
    }

    public CategoryFacade getCategoryFacade() {
        if(categoryFacade==null){
            categoryFacade = new CategoryFacade();
        }
        return categoryFacade;
    }

    public LabelFacade getLabelFacade() {
        if(labelFacade==null){
            labelFacade = new LabelFacade();
        }
        return labelFacade;
    }
    
    public String getMenuTitle() {
        return MenuString.CATEGORY;
    }

    public RSSChannelFacade getRssChannelFacade() {
        if(rssChannelFacade==null)
            rssChannelFacade = new RSSChannelFacade();
        return rssChannelFacade;
    }

}
